home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Macintosh Common Lisp Related / interfaces / PInterface Translator / OCE Interfaces / DigitalSignature.p < prev    next >
Encoding:
Text File  |  1993-09-16  |  15.5 KB  |  404 lines  |  [TEXT/MPS ]

  1. {***********************************************************
  2.  
  3. Created: Sunday, February 7, 1993 at 4:54PM
  4.  DigitalSignature.p
  5.  Pascal Interface to the Macintosh Libraries
  6.  
  7.     ••• Apple Confidential •••
  8.     ••• Provided with AOCE Beta seed under license agreement with   •••
  9.     ••• Apple Computer, Inc.  Use for any purpose other than        •••
  10.     ••• development of AOCE-based Macintosh products is a violation •••
  11.     ••• of the license agreement.                                   •••
  12.  
  13.   Copyright Apple Computer, Inc. 1990-1993
  14.   All rights reserved
  15.  
  16. ***********************************************************}
  17.  
  18.  
  19. {$IFC UNDEFINED UsingIncludes}
  20. {$SETC UsingIncludes:= 0}
  21. {$ENDC}
  22.  
  23. {$IFC NOT UsingIncludes}
  24.     UNIT DigitalSignature;
  25.     INTERFACE
  26. {$ENDC}
  27.  
  28. {$IFC UNDEFINED UsingDigitalSignature}
  29. {$SETC UsingDigitalSignature:= 1}
  30.  
  31. {$I+}
  32. {$SETC DigitalSignatureIncludes:= UsingIncludes}
  33. {$SETC UsingIncludes:= 1}
  34.  
  35.  
  36. {$IFC UNDEFINED UsingFiles}
  37. {$I $$Shell(PInterfaces)Files.p}
  38. {$ENDC}
  39.  
  40. {$IFC UNDEFINED UsingMemory}
  41. {$I $$Shell(PInterfaces)Memory.p}
  42. {$ENDC}
  43.  
  44. {$IFC UNDEFINED UsingTypes}
  45. {$I $$Shell(PInterfaces)Types.p}
  46. {$ENDC}
  47.  
  48. {$SETC UsingIncludes:= DigitalSignatureIncludes}
  49.  
  50.  
  51. { —————————————————————————— TRAP SELECTORS —————————————————————————— }
  52.  
  53. CONST
  54. kSIGNewContext                    = 1900;
  55. kSIGDisposeContext                = 1901;
  56.  
  57. kSIGSignPrepare                    = 1902;
  58. kSIGSign                        = 1903;
  59.  
  60. kSIGVerifyPrepare                = 1904;
  61. kSIGVerify                        = 1905;
  62.  
  63. kSIGDigestPrepare                = 1906;
  64. kSIGDigest                        = 1907;
  65.  
  66. kSIGProcessData                    = 1908;
  67.  
  68. kSIGShowSigner                    = 1909;
  69. kSIGGetSignerInfo                = 1910;
  70. kSIGGetCertInfo                    = 1911;
  71. kSIGGetCertNameAttributes        = 1912;
  72. kSIGGetCertIssuerNameAttributes    = 1913;
  73.  
  74. kSIGFileIsSigned                = 2500;
  75. kSIGSignFile                    = 2501;
  76. kSIGVerifyFile                    = 2502;
  77.  
  78.  
  79. { values of SIGNameAttributeType }
  80. kSIGCountryCode = 0;
  81. kSIGOrganization = 1;
  82. kSIGStreetAddress = 2;
  83. kSIGState = 3;
  84. kSIGLocality = 4;
  85. kSIGCommonName = 5;
  86. kSIGTitle = 6;
  87. kSIGOrganizationUnit = 7;
  88. kSIGPostalCode = 8;
  89.  
  90. TYPE
  91. SIGNameAttributeType = INTEGER;
  92.  
  93. {
  94. Certificate status codes returned in SIGCertInfo or SIGSignerInfo from
  95. either SIGGetCertInfo or SIGGetSignerInfo respectively. kSIGValid means that
  96. the certificate is currently valid. kSIGPending means the certificate is
  97. currently not valid - but will be.  kSIGExpired means the certificate has
  98. expired. A time is always associated with a SIGCertStatus.  In each case the
  99. time has a specific interpretation.  When the status is kSIGValid the time is
  100. when the certificate will expire. When the status is kSIGPending the time is
  101. when the certificate will become valid. When the status is kSIGExpired the time
  102. is when the certificate expired. In the SIGCertInfo structure, the startDate
  103. and endDate fields hold the appropriate date information.  In the SIGSignerInfo
  104. structure, this information is provided in the certSetStatusTime field. In the
  105. SIGSignerInfo struct, the status time is actually represented by the SIGSignatureStatus
  106. field which can contain any of the types below. NOTE: The only time you will get
  107. a kSIGInvalid status is when it pertains to a SIGSignatureStatus field and only when
  108. you get a signature that was created after the certificates expiration date, something
  109. we are not allowing on the Mac but that may not be restricted on other platforms. Also,
  110. it will not be possible to get a kSIGPending value for SIGSignatureStatus on the Mac but
  111. possibly allowed by other platforms.
  112. }
  113.  
  114. { Values for SIGCertStatus or SIGSignatureStatus }
  115. CONST
  116. kSIGValid = 0;                { possible for either a SIGCertStatus or SIGSignatureStatus }
  117. kSIGPending = 1;            { possible for either a SIGCertStatus or SIGSignatureStatus }
  118. kSIGExpired = 2;            { possible for either a SIGCertStatus or SIGSignatureStatus }
  119. kSIGInvalid = 3;            { possible only for a SIGSignatureStatus }
  120.  
  121.  
  122. TYPE
  123. SIGCertStatus = INTEGER;
  124. SIGSignatureStatus = INTEGER;
  125.  
  126. { Gestalt selector code - returns toolbox version in low-order word }
  127. CONST
  128. gestaltDigitalSignatureVersion = 'dsig';
  129.  
  130.  
  131. { Number of bytes needed for a digest record when using SIGDigest }
  132. kSIGDigestSize = 16;
  133.  
  134. TYPE
  135. SIGDigestData = PACKED ARRAY[1..kSIGDigestSize] OF Byte;
  136. SIGDigestDataPtr = ^SIGDigestData;
  137.  
  138. SIGCertInfo = RECORD
  139.     startDate: LONGINT;                    { cert start validity date }
  140.     endDate: LONGINT;                    { cert end validity date }
  141.     certStatus: SIGCertStatus;            { see comment on SIGCertStatus for definition }
  142.     certAttributeCount: LONGINT;        { number of name attributes in this cert }
  143.     issuerAttributeCount: LONGINT;        { number of name attributes in this certs issuer }
  144.     serialNumber: Str255;                { cert serial number }
  145.     END;
  146.  
  147. SIGCertInfoPtr = ^SIGCertInfo;
  148.  
  149. SIGSignerInfo = RECORD
  150.     signingTime: LONGINT;                { time of signing }
  151.     certCount: LONGINT;                    { number of certificates in the cert set }
  152.     certSetStatusTime: LONGINT;            { Worst cert status time. See comment on
  153.                                             SIGCertStatus for definition }
  154.     signatureStatus: SIGSignatureStatus;{ The status of the signature. See comment on
  155.                                             SIGCertStatus for definition}
  156.     END;
  157.  
  158. SIGSignerInfoPtr = ^SIGSignerInfo;
  159.  
  160. SIGNameAttributesInfo = RECORD
  161.     onNewLevel: BOOLEAN;
  162.     attributeType: SIGNameAttributeType;
  163.     attributeScript: ScriptCode;
  164.     attribute: Str255;
  165.     END;
  166.  
  167. SIGNameAttributesInfoPtr = ^SIGNameAttributesInfo;
  168.  
  169. SIGContextPtr = Ptr;
  170. SIGSignaturePtr = Ptr;
  171.  
  172. {
  173. Certificates are always in order. That is, the signers cert is always 0, the
  174. issuer of the signers cert is always 1 etc… to the number of certificates-1.
  175. You can use this constant for readability in your code.
  176. }
  177.  
  178. CONST
  179. kSIGSignerCertIndex = 0;
  180.  
  181. {
  182. Call back procedure supplied by developer, return false to cancel the current process.
  183. }
  184.  
  185. TYPE
  186. SIGStatusProcPtr = ProcPtr;                { FUNCTION SIGStatusProcPtr(): BOOLEAN;}
  187.  
  188. {
  189. Resource id of standard signature icon suite, all sizes and colors are available.
  190. }
  191.  
  192. CONST
  193. kSIGSignatureIconResID        = -5045;
  194.  
  195. { ——————————————————————————————— CONTEXT CALLS ——————————————————————————————— }
  196. {
  197. To use the Digital Signature toolbox you will need a SIGContextPtr.  To create
  198. a SIGContextPtr you simply call SIGNewContext and it will create and initialize
  199. a context for you.  To free the memory occupied by the context and invalidate
  200. its internal data, call SIGDisposeContext. An initialized context has no notion
  201. of the type of operation it will be performing however, once you call
  202. SIGSignPrepare SIGVerifyPrepare, or SIGDigestPrepare, the contexts operation
  203. type is set and to switch  to another type of operation will require creating a
  204. new context. Be sure to pass the same context to corresponding toolbox calls
  205. (ie: SIGSignPrepare; SIGProcessData, SIGSign)  in other words mixing lets say
  206. signing and verify calls with the same context is not allowed.
  207. }
  208.  
  209.  
  210. FUNCTION SIGNewContext(VAR context: SIGContextPtr): OSErr;
  211.     INLINE $203C, 2, kSIGNewContext, $AA5D;
  212.  
  213. FUNCTION SIGDisposeContext(context: SIGContextPtr): OSErr;
  214.     INLINE $203C, 2, kSIGDisposeContext, $AA5D;
  215.  
  216.  
  217. { ——————————————————————————————— SIGNING CALLS ——————————————————————————————— }
  218. {
  219. Once you have created a SIGContextPtr, you create a signature by calling
  220. SIGSignPrepare once, followed by n calls to SIGProcessData, followed by one call
  221. to SIGSign. To create another signature on different data but for the same
  222. signer, don't dispose of the context and call SIGProcessData for the new data
  223. followed by a call SIGSign again. In this case the signer will not be prompted
  224. for their signer and password again as it was already provided.  Once you call
  225. SIGDisposeContext, all signer information will be cleared out of the context and
  226. the signer will be re-prompted.  The signer file FSSpecPtr should be set to nil
  227. if you want the toolbox to use the last signer by default or prompt for a signer
  228. if none exists.  The prompt parameter can be used to pass a string to be displayed
  229. in the dialog that prompts the user for their password.  If the substring "^1"
  230. (without the quotes) is in the prompt string, then the toolbox will replace it
  231. with the name of the signer from the signer selected by the user.  If an empty
  232. string is passed, the following default string will be sent to the toolbox
  233. "\pSigning as ^1.".  You can call any of the utility routines after SIGSignPrepare
  234. or SIGSign to get information about the signer or certs.
  235. }
  236.  
  237. FUNCTION SIGSignPrepare(context: SIGContextPtr; signerFile: FSSpecPtr;
  238.     prompt: StringPtr; VAR signatureSize: Size): OSErr;
  239.     INLINE $203C, 8, kSIGSignPrepare, $AA5D;
  240.  
  241. FUNCTION SIGSign(context: SIGContextPtr; signature: SIGSignaturePtr;
  242.     statusProc: SIGStatusProcPtr): OSErr;
  243.     INLINE $203C, 6, kSIGSign, $AA5D;
  244.  
  245.  
  246. { ——————————————————————————————— VERIFYING CALLS ——————————————————————————————— }
  247. {
  248. Once you have created a SIGContextPtr, you verify a signature by calling
  249. SIGVerifyPrepare  once, followed by n calls to SIGProcessData, followed by one
  250. call to SIGVerify. Check the return code from SIGVerify to see if the signature
  251. verified or not (noErr is returned on  success otherwise the appropriate error
  252. code).  Upon successfull verification, you can call any of the utility routines
  253. to find out who signed the data.
  254. }
  255.  
  256. FUNCTION SIGVerifyPrepare(context: SIGContextPtr; signature: SIGSignaturePtr;
  257.     signatureSize: Size; statusProc: SIGStatusProcPtr): OSErr;
  258.     INLINE $203C, 8, kSIGVerifyPrepare, $AA5D;
  259.  
  260. FUNCTION SIGVerify(context: SIGContextPtr): OSErr;
  261.     INLINE $203C, 2, kSIGVerify, $AA5D;
  262.  
  263. { —————————————————————————————— DIGESTING CALLS —————————————————————————————— }
  264. {
  265. Once you have created a SIGContextPtr, you create a digest by calling
  266. SIGDigestPrepare once, followed by n calls to SIGProcessData, followed by one
  267. call to SIGDigest.  You can dispose of the context after SIGDigest as the
  268. SIGDigestData does not reference back into it.  SIGDigest returns the digest in
  269. digest.
  270. }
  271.  
  272. FUNCTION SIGDigestPrepare(context: SIGContextPtr): OSErr;
  273.     INLINE $203C, 2, kSIGDigestPrepare, $AA5D;
  274.  
  275. FUNCTION SIGDigest(context: SIGContextPtr; digest: SIGDigestData): OSErr;
  276.     INLINE $203C, 4, kSIGDigest, $AA5D;
  277.  
  278.  
  279. { —————————————————————————————— PROCESSING DATA —————————————————————————————— }
  280. {
  281. To process data during a digest, sign, or verify operation call SIGProcessData
  282. as many times as necessary and with any sized blocks of data.  The data needs to
  283. be processed in the same order during corresponding sign and verify operations
  284. but does not need to be processed in the same sized chunks (i.e., the toolbox
  285. just sees it as a continuous bit stream).
  286. }
  287.  
  288. FUNCTION SIGProcessData(context: SIGContextPtr; data: UNIV Ptr; dataSize: Size): OSErr;
  289.     INLINE $203C, 6, kSIGProcessData, $AA5D;
  290.  
  291.  
  292. { ——————————————————————————————— UTILITY CALLS ——————————————————————————————— }
  293. {
  294. Given a context that has successfully performed a verification SIGShowSigner
  295. will  display a modal dialog with the entire distinguished name of the person
  296. who signed the data. the prompt (if supplied) will appear at the top of the
  297. dialog.  If no prompt is specified, the default prompt "\pVerification
  298. Successfull." will appear.
  299.  
  300. Given a context that has been populated by calling SIGSignPrepare, SIGSign or a
  301. successful SIGVerify, you can make the remaining utility calls:
  302.  
  303. SIGGetSignerInfo will return the SignerInfo record.  The certCount can be used
  304. to index into the certificate set when calling SIGGetCertInfo,
  305. SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes. The signingTime is
  306. only defined if the call is made after SIGSign  or SIGVerify. The certSetStatus
  307. will tell you the best status of the entire certificate set while
  308. certSetStatusTime will correspond to the time associated with that status (see
  309. definitions above).
  310.  
  311. SIGGetCertInfo will return the SIGCertInfo record when given a valid index into
  312. the cert set in  certIndex.  Note: The cert at index kSIGSignerCertIndex is
  313. always the signers certificate.  The  serial number, start date and end date
  314. are there should you wish to display that info.  The  certAttributeCount and
  315. issuerAttributeCount provide the number of parts in the name of that certificate
  316. or that certificates issuer respectively.  You use these numbers to index into
  317. either SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes to retrieve
  318. the name. The certStatus will tell you the status of the certificate while
  319. certStatusTime will correspond to the time associated with that status (see
  320. definitions above).
  321.  
  322. SIGGetCertNameAttributes and SIGGetCertIssuerNameAttributes return name parts
  323. of the certificate at  certIndex and attributeIndex.  The newLevel return value
  324. tells you wether the name attribute returned is at the same level in the name
  325. hierarchy as the previous attribute.  The type return value tells you  the type
  326. of attribute returned. nameAttribute is the actual string containing the name
  327. attribute.   So, if you wanted to display the entire distinguished name of the
  328. person who's signature was just validated you could do something like this;
  329.  
  330.     (…… variable declarations and verification code would preceed this sample ……)
  331.  
  332.     error = SIGGetCertInfo(verifyContext, kSIGSignerCertIndex, &certInfo);
  333.     HandleErr(error);
  334.  
  335.     for (i = 0; i <= certInfo.certAttributeCount-1; i++)
  336.         error = SIGGetCertNameAttributes(verifyContext, kSIGSignerCertIndex, i, &newLevel, &type, theAttribute);
  337.         HandleErr(error);
  338.         DisplayNamePart(theAttribute, type, newLevel);
  339. }
  340.  
  341. FUNCTION SIGShowSigner(context: SIGContextPtr; prompt: StringPtr): OSErr;
  342.     INLINE $203C, 4, kSIGShowSigner, $AA5D;
  343.  
  344. FUNCTION SIGGetSignerInfo(context: SIGContextPtr; VAR signerInfo: SIGSignerInfo): OSErr;
  345.     INLINE $203C, 4, kSIGGetSignerInfo, $AA5D; 
  346.  
  347. FUNCTION SIGGetCertInfo(
  348.         context: SIGContextPtr; certIndex: LONGINT; VAR certInfo: SIGCertInfo): OSErr;
  349.     INLINE $203C, 6, kSIGGetCertInfo, $AA5D; 
  350.  
  351. FUNCTION SIGGetCertNameAttributes(context: SIGContextPtr; certIndex: LONGINT;
  352.         attributeIndex: LONGINT; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  353.     INLINE $203C, 8, kSIGGetCertNameAttributes, $AA5D; 
  354.  
  355. FUNCTION SIGGetCertIssuerNameAttributes(context: SIGContextPtr; certIndex: LONGINT;
  356.         attributeIndex: LONGINT; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  357.     INLINE $203C, 8, kSIGGetCertIssuerNameAttributes, $AA5D; 
  358.  
  359.  
  360.  
  361. { ——————————————————————————— FILE SIGN & VERIFY CALLS —————————————————————————— }
  362. {
  363. These calls allow you to detect the presence of a standard signtaure in a file as
  364. well as sign and verify files in a standard way.  An example of this is the Finder,
  365. which uses these calls to allow the user to "drop sign" a file.
  366.  
  367. To detect if a file is signed in the standard way, pass the FSSpec of the file to SIGFileIsSigned.
  368. A result of noErr means the file is in fact signed, otherwise, a kSIGNoSignature error will
  369. be returned.
  370.  
  371. Once you have created a SIGContextPtr, you can make calls to either sign or verify a file in
  372. a standard way:
  373.  
  374. To sign a file, call SIGSignPrepare followed by 'n' number of calls to SIGSignFile,
  375. passing it the file spec for each file you wish to sign in turn.  You supply the context, the signature
  376. size that was returned from SIGSignPrepare and an optional call back proc.  The call will take care of all
  377. the processing of data and affixing the signature to the file. If a signature already exists in the file,
  378. it is replaced with the newly created signature.
  379.  
  380. To verify a file that was signed using SIGSignFile, call SIGVerifyFile passing it a new context and
  381. the file spec.  Once this call has completed, if the verification is successfull, you can pass the context
  382. to SIGShowSigner to display the name of the person who signed the file.
  383. }
  384.  
  385. FUNCTION SIGFileIsSigned(fileSpec: FSSpec): OSErr;
  386.     INLINE $203C, 2, kSIGFileIsSigned, $AA5D;
  387.  
  388. FUNCTION SIGSignFile(
  389.         context: SIGContextPtr; signatureSize: Size; fileSpec: FSSpec;
  390.         statusProc: SIGStatusProcPtr): OSErr;
  391.     INLINE $203C, 8, kSIGSignFile, $AA5D;
  392.  
  393. FUNCTION SIGVerifyFile(
  394.         context: SIGContextPtr; fileSpec: FSSpec; statusProc: SIGStatusProcPtr): OSErr;
  395.     INLINE $203C, 6, kSIGVerifyFile, $AA5D;
  396.  
  397.  
  398.  
  399. {$ENDC}    { UsingDigitalSignature }
  400.  
  401. {$IFC NOT UsingIncludes}
  402.     END.
  403. {$ENDC}
  404.